package cn.pegasus.teamwork.advertisement.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.advertisement.entity.BussAdvertInfo;
import cn.pegasus.teamwork.advertisement.mapper.BussAdvertInfoMapper;
import cn.pegasus.teamwork.advertisement.service.BussAdvertInfoService;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.manager.service.SysUcenterAppsService;
import cn.pegasus.teamwork.sysconfig.entity.SysConfigDictionary;
import cn.pegasus.teamwork.sysconfig.service.SysConfigDictionaryService;
import cn.pegasus.teamwork.util.OSSClientUtil;

/**
 * @ClassName: SysAdminFunctionController
 * @Description: 菜单管理
 * @author buyuer
 * @date 2015年11月10日 下午8:18:07
 * 
 */
@RequestMapping("/advertis")
@Controller
public class AdvertisementController extends
		BaseManagerController<BussAdvertInfo, BussAdvertInfoMapper> {

	private static final Logger logger = LoggerFactory
			.getLogger(AdvertisementController.class);

	@Autowired
	private BussAdvertInfoService service;

	@Autowired
	private SysUcenterAppsService appsService;


	@Autowired
	private SysConfigDictionaryService dictionaryService;

	/**
	 * 查询列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("advertis/adver-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussAdvertInfo> dicList = service.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询广告位列表异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 添加新增页面
	 */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Integer id)throws Exception{
		 List<SysConfigDictionary> config = dictionaryService.getDicList("advert_position");
		 if(null == type){
			 ModelAndView view = new ModelAndView("advertis/adver-add");
			 view.addObject("list", config);
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("advertis/adver-edit");
			 BussAdvertInfo news = service.findInfo(id);
			 view.addObject("info", news);
			 view.addObject("list", config);
			 return view;
		 } 
	 }
	 
	 /**
	  * 新增
	  */
	 @ResponseBody
	 @RequestMapping(value = "/addAdv", method = RequestMethod.POST)
	 public String addAdv(HttpServletRequest request,
			 @RequestParam(value = "advertPosition", required = false) String advertPosition,
			 @RequestParam(value = "advertName", required = false) String advertName,
			 @RequestParam(value = "advertResourceUrl", required = false) String advertResourceUrl,
			 @RequestParam(value = "advertRemark", required = false) String advertRemark,
			 @RequestParam(value = "advertSeq", required = false) Integer advertSeq){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussAdvertInfo info = new BussAdvertInfo();
        	info.setAdvertPosition(advertPosition);
        	info.setAdvertName(advertName);
        	info.setAdvertResourceUrl(advertResourceUrl);
        	info.setAdvertRemark(advertRemark);
        	info.setAdvertSeq(advertSeq);
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String advertImage = util.uploadImg2Oss(file);
            	info.setAdvertImage(advertImage);
        	}
        	service.addAdv(getAdmin().getId(), info);
        } catch (Exception e) {
            logger.error("新增优惠券异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 修改
	  */
	 @ResponseBody
	 @RequestMapping(value="/updateAdv", method = RequestMethod.POST)
	 public String updateAdv(HttpServletRequest request,
			 @RequestParam(value = "advertPosition", required = false) String advertPosition,
			 @RequestParam(value = "advertName", required = false) String advertName,
			 @RequestParam(value = "advertResourceUrl", required = false) String advertResourceUrl,
			 @RequestParam(value = "advertRemark", required = false) String advertRemark,
			 @RequestParam(value = "advertSeq", required = false) Integer advertSeq,
			 @RequestParam(value = "advertState", required = false) Integer advertState,
			 @RequestParam(value = "id", required = false) Integer id){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussAdvertInfo info = new BussAdvertInfo();
        	info.setAdvertPosition(advertPosition);
        	info.setAdvertName(advertName);
        	info.setAdvertResourceUrl(advertResourceUrl);
        	info.setAdvertRemark(advertRemark);
        	info.setAdvertSeq(advertSeq);
        	info.setAdvertState(advertState);
        	info.setId(id);
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String advertImage = util.uploadImg2Oss(file);
            	info.setAdvertImage(advertImage);
        	}
        	service.updateAdv(getAdmin().getId(), info);
        } catch (Exception e) {
            logger.error("修改优惠券信息异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 下架
	  */
	 @ResponseBody
	 @RequestMapping(value="/delete")
	 public String delete(@RequestParam(value="id",required = false) Integer id){
        try {
        	service.deleteAdv(getAdmin().getId(), id);
        } catch (Exception e) {
            logger.error("删除区域异常", e);
            return "1001";
        }
        return "200";
	 }

}
